var num = 0;
var i = 0;
var PicturePath1 = "";
var PicturePath2 = "";
var PicturePath3 = "";
var form;

jQuery(function () {
    layui.use(['layer', 'form'], function () {
        form = layui.form();
        init();
        $("#deadline").datetimepicker({
            timepicker:false,
            format:'Y/m/d',
            lang:"ch"
        });

    });
});
function init() {
    InitParentCate();
    form.on('checkbox(isrequirefile)', function (data) {
        if (data.elem.checked) {
            $("#isrequirefile").val("1")
        } else {
            $("#isrequirefile").val("0");
        }
    });
    form.on('checkbox(isrequireimg)', function (data) {
        if (data.elem.checked) {
            $("#isrequireimg").val("1")
        } else {
            $("#isrequireimg").val("0");
        }
    });
    form.on('checkbox(isregister)', function (data) {
        if (data.elem.checked) {
            $("#isregister").val("1")
        } else {
            $("#isregister").val("0");
        }
    });
    form.on('checkbox(ischild)', function (data) {
        if (data.elem.checked) {
            $("#ischild").val("1")
        } else {
            $("#ischild").val("0");
        }
    });
    getArea(1,1);
    $("#btn").click(next);
}
var getArea = function(parent,servel){
    $.ajax({
        url:server_url+'rest/areaManager/getByParent.do?parentId='+parent,
        data:{ran:Math.random()},
        type:'get',
        error:function(){alert(contants.systemError);},
        success:function(msg){
            //console.log(msg);
            var areas = msg.data;
            var s = '';
            for(var i=0;i<areas.length;i++){
                s +='<option value="'+areas[i].areaId+','+areas[i].areaName+'">'+areas[i].areaName+'</option>';
            }
            if(servel ==1){
                s= '<option value="">请选择省</option>' +s;
                $("#consProvince1").html(s);
                form.render("select");
                form.on('select(consProvince)', function(data){
                    var pro = data.value.substring(0,data.value.indexOf(","));
                    $("#consProvince").val(data.value.substring(data.value.indexOf(",")+1));
                    getArea(pro,2);
                });

            }else if(servel == 2){
                s= '<option value="">请选择市</option>' +s;
                $("#consCity1").html(s);
                form.render("select");
                form.on('select(consCity)', function(data){
                    $("#consCity").val(data.value.substring(data.value.indexOf(",")+1));
                    getArea(data.value.substring(0,data.value.indexOf(",")),3)
                });
            }else if(servel == 3){
                s= '<option value="">请选择县</option>' +s;
                $("#consCountry1").html(s);
                form.render("select");
                form.on('select(consCountry)', function(data){
                    var pro = data.value;
                    $("#consCountry").val(data.value.substring(data.value.indexOf(",")+1));
                });
            }

        }

    })
}
var b = false;
function next() {
    var parentId = $("#parentId").val()
    var cateId = $("#cateId").val()
    var specId = $("#specId").val()
    var productName = $("#productname").val()

    if ($("#deadline").val() == null || $("#deadline").val()== '') {
        easyAlert("请选择有效期限！");
        return false;
    }
    if (parentId == null || parentId== '') {
        easyAlert("请选择一级分类！");
        return false;
    } else if (cateId == null || cateId == '') {
        easyAlert("请选择二级分类！");
        return false;
    } else if (specId == null || specId == '') {
        easyAlert("请选择类别！");
        return false;
    } else if (productName == null || productName == '') {
        easyAlert("请填写商品名！");
        return false;
    }
    var minprice = $("#minprice").val();
    if (minprice == "") {
        easyAlert("请输入价格！");
        return false;
    }
    if (!isPriceNumber(minprice)) {
        easyAlert("格式不正确！请输入正确的价格格式！");
        return false;
    }
    var maxprice = $("#maxprice").val();
    if (maxprice == "") {
        easyAlert("请输入价格！");
        return false;
    }
    if (!isPriceNumber(maxprice)) {
        easyAlert("格式不正确！请输入正确的价格格式！");
        return false;
    }
    if ($("#consProvince").val() == null || $("#consProvince").val()== '') {
        easyAlert("请选择收货省！");
        return false;
    }
    if ($("#consCity").val() == null || $("#consCity").val()== '') {
        easyAlert("请选择收货市！");
        return false;
    }
    if ($("#consCountry").val() == null || $("#consCountry").val()== '') {
        easyAlert("请选择收货县！");
        return false;
    }
    if ($("#address").val() == null || $("#address").val()== '') {
        easyAlert("请填写收货详址！");
        return false;
    }
    var deliveryArea = $("#consProvince").val()+$("#consCity").val()+$("#consCountry").val();
    if(b){
        return;
    }
    b = true;
    $.ajax({
        url: server_url + '/rest/buyer/demand/publicDemand.do?userId='+userid+"&usersid="+userid+"&deliveryarea="+deliveryArea+"&ran="+Math.random(),
        data: $("form").serialize(),
        type:"post",
        success: function (data) {
            easyAlert("发布成功！");
            window.location.href = server_url + '/buyer/demand/source.do';
        },
        error:function(){
            easyAlert("出了点问题")
            b = false;
        }
    })
}
function InitParentCate() {
    $.ajax({
        url: server_url + '/rest/productCate/selectParentCate.do',
        data:{ran:Math.random()},
        type: 'get',
        async: false,
        success: function (data) {
            if (data.retCode == "0000") {
                for (var i = 0; i < data.data.length; i++) {
                    $("#parentId1").append("<option  value='" + data.data[i].cateId + "'>" + data.data[i].cateName + "</option>");
                }
                form.render("select")
                form.on("select(parentId)", function (data) {
                    var pro = data.value;
                    $("#parentId").val(pro);
                    sencondCateLoad();
                })
            }
            else {
                easyDialog.open({
                    container: {
                        header: '提示',
                        content: data.retMsg
                    },
                    autoClose: 2000
                });
            }
        }
    });
}

function sencondCateLoad() {
    var parentId = $("#parentId").val();
    $.ajax({
        url: server_url + '/rest/productCate/selectChildCateKv.do?cateId=' + parentId,
        data:{ran:Math.random()},
        type: 'post',
        success: function (data) {
            $("#cateId1").html("");
            if (data.retCode == "0000") {
                var html = '<option value="">请选择</option>';
                for (var i = 0; i < data.data.length; i++) {
                    html += "<option value=" + data.data[i].cateId + ">" + data.data[i].cateName + "</option>";
                }
                $(html).appendTo("#cateId1");
                form.render("select")
                form.on("select(cateId)", function (data) {
                    var pro = data.value;
                    $("#cateId").val(pro);
                    changeSpecId();
                    //changeProductName();
                });
            }
            else {
                easyDialog.open({
                    container: {
                        header: '提示',
                        content: data.retMsg
                    },
                    autoClose: 2000
                });
            }
        }
    });
}

//切换子分类选择属性名称
function changeSpecId() {
    var cateId = $("#cateId").val();
    $.ajax({
        url: server_url + '/rest/product/selectByCateId.do?ran=' + Math.random(),
        type: 'post',
        data: {cateId: cateId},
        success: function (data) {
            if (data.retCode == "0000") {
                InitSpec(data);
                form.render("select")
                form.on("select(specId)", function (data) {
                    var pro = data.value;
                    $("#specId").val(pro);
                })
            }
            else {
                easyDialog.open({
                    container: {
                        header: '提示',
                        content: data.retMsg
                    },
                    autoClose: 2000
                });
            }
        }
    });
}

//输出Dom
function InitSpec(data) {
    var html = '<option value="">请选择</option>';
    for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].specName == "类别") {
            html += "<option value=" + data.data[i].specValueId + ">" + data.data[i].specValueName + "</option>";
        }
    }
    $("#specId1").html(html);
}

function toSkuProductList() {
    window.location.href = "index.do?ran=" + Math.random();
};